package com.ruoyi.goods.service.impl;

import java.util.List;

import com.alibaba.druid.sql.visitor.functions.If;
import com.ruoyi.goods.domain.Preempt;
import com.ruoyi.goods.domain.ProductCategories;
import com.ruoyi.goods.domain.Sales;
import com.ruoyi.goods.mapper.PreemptMapper;
import com.ruoyi.goods.mapper.ProductCategoriesMapper;
import com.ruoyi.goods.mapper.SalesMapper;
import com.ruoyi.goods.utils.SnowflakeIdUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.goods.mapper.ProductsMapper;
import com.ruoyi.goods.domain.Products;
import com.ruoyi.goods.service.IProductsService;

/**
 * 商品管理Service业务层处理
 *
 * @author mabo
 * @date 2025-01-06
 */
@Service
public class ProductsServiceImpl implements IProductsService
{
    @Autowired
    private ProductsMapper productsMapper;

    @Autowired
    private SalesMapper salesMapper;

    @Autowired
    private PreemptMapper  preemptMapper;
    @Autowired
    private ProductCategoriesMapper productCategoriesMapper;
    /**
     * 查询商品管理
     *
     * @param id 商品管理主键
     * @return 商品管理
     */
    @Override
    public Products selectProductsById(Long id)
    {
        return productsMapper.selectProductsById(id);
    }

    /**
     * 查询商品管理列表
     *
     * @param products 商品管理
     * @return 商品管理
     */
    @Override
    public List<Products> selectProductsList(Products products)
    {
        return productsMapper.selectProductsList(products);
    }

    /**
     * 新增商品管理
     *
     * @param products 商品管理
     * @return 结果
     */
    @Override
    public int insertProducts(Products products)

    {
        String description = products.getDescription();


        int state = products.getState();
        if (state==1){
            Preempt  preempt = new Preempt();
            preempt.setProductId(products.getId());
             preemptMapper.insertPreempt(preempt);
        }
        // 使用 SecurityUtils 获取当前用户 ID)
        int result = productsMapper.insertProducts(products);

        // 获取插入后的产品ID
        Long productId = products.getId(); // 确保products对象中的ID在插入后被正确更新

        // 只有当产品插入成功且产品ID不为null时才插入销售记录
        if (result > 0 && productId != null) {


            Sales sales = new Sales();
            sales.setProductId(productId); // 设置新插入商品的ID
            // 设置其他必要的销售记录的字段，如果有的话
            salesMapper.insertSales(sales);
        }

        return result; // 返回商品插入的结果
    }

    /**
     * 修改商品管理
     *
     * @param products 商品管理
     * @return 结果
     */
    @Override
    public int updateProducts(Products products)
    {
        int state = products.getState();
        if (state==1){
            Preempt  preempt = new Preempt();
            preempt.setProductId(products.getId());

            preemptMapper.insertPreempt(preempt);
        }
        return productsMapper.updateProducts(products);
    }

    /**
     * 批量删除商品管理
     *
     * @param ids 需要删除的商品管理主键
     * @return 结果
     */
    @Override
    public int deleteProductsByIds(Long[] ids)
    {
        return productsMapper.deleteProductsByIds(ids);
    }

    /**
     * 删除商品管理信息
     *
     * @param id 商品管理主键
     * @return 结果
     */
    @Override
    public int deleteProductsById(Long id)
    {
        return productsMapper.deleteProductsById(id);
    }

    @Override
    public List<Products> selectBytype(Long id) {
        return productsMapper.selectBytype(id);
    }
    @Override
    public List<Products> selectProductsone(Products products){
        return productsMapper.selectProductsone(products);
    }
}
package com.ruoyi.goods.service.impl;

import java.util.List;

import com.alibaba.druid.sql.visitor.functions.If;
import com.ruoyi.goods.domain.Preempt;
import com.ruoyi.goods.domain.ProductCategories;
import com.ruoyi.goods.domain.Sales;
import com.ruoyi.goods.mapper.PreemptMapper;
import com.ruoyi.goods.mapper.ProductCategoriesMapper;
import com.ruoyi.goods.mapper.SalesMapper;
import com.ruoyi.goods.utils.SnowflakeIdUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.goods.mapper.ProductsMapper;
import com.ruoyi.goods.domain.Products;
import com.ruoyi.goods.service.IProductsService;

/**
 * 商品管理Service业务层处理
 *
 * @author mabo
 * @date 2025-01-06
 */
@Service
public class ProductsServiceImpl implements IProductsService
{
    @Autowired
    private ProductsMapper productsMapper;

    @Autowired
    private SalesMapper salesMapper;

    @Autowired
    private PreemptMapper  preemptMapper;
    @Autowired
    private ProductCategoriesMapper productCategoriesMapper;
    /**
     * 查询商品管理
     *
     * @param id 商品管理主键
     * @return 商品管理
     */
    @Override
    public Products selectProductsById(Long id)
    {
        return productsMapper.selectProductsById(id);
    }

    /**
     * 查询商品管理列表
     *
     * @param products 商品管理
     * @return 商品管理
     */
    @Override
    public List<Products> selectProductsList(Products products)
    {
        return productsMapper.selectProductsList(products);
    }

    /**
     * 新增商品管理
     *
     * @param products 商品管理
     * @return 结果
     */
    @Override
    public int insertProducts(Products products)

    {
        String description = products.getDescription();
        products.setId(Long.valueOf(String.valueOf(new SnowflakeIdUtils(5,8).nextId())));

        int state = products.getState();
        if (state==1){
            Preempt  preempt = new Preempt();
            preempt.setProductId(products.getId());
             preemptMapper.insertPreempt(preempt);
        }
        // 使用 SecurityUtils 获取当前用户 ID)
        int result = productsMapper.insertProducts(products);

        // 获取插入后的产品ID
        Long productId = products.getId(); // 确保products对象中的ID在插入后被正确更新

        // 只有当产品插入成功且产品ID不为null时才插入销售记录
        if (result > 0 && productId != null) {


            Sales sales = new Sales();
            sales.setProductId(productId); // 设置新插入商品的ID
            // 设置其他必要的销售记录的字段，如果有的话
            salesMapper.insertSales(sales);
        }

        return result; // 返回商品插入的结果
    }

    /**
     * 修改商品管理
     *
     * @param products 商品管理
     * @return 结果
     */
    @Override
    public int updateProducts(Products products)
    {
        int state = products.getState();
        if (state==1){
            Preempt  preempt = new Preempt();
            preempt.setProductId(products.getId());

            preemptMapper.insertPreempt(preempt);
        }
        return productsMapper.updateProducts(products);
    }

    /**
     * 批量删除商品管理
     *
     * @param ids 需要删除的商品管理主键
     * @return 结果
     */
    @Override
    public int deleteProductsByIds(Long[] ids)
    {
        return productsMapper.deleteProductsByIds(ids);
    }

    /**
     * 删除商品管理信息
     *
     * @param id 商品管理主键
     * @return 结果
     */
    @Override
    public int deleteProductsById(Long id)
    {
        return productsMapper.deleteProductsById(id);
    }

    @Override
    public List<Products> selectBytype(Long id) {
        return productsMapper.selectBytype(id);
    }
    @Override
    public List<Products> selectProductsone(Products products){
        return productsMapper.selectProductsone(products);
    }
}
